package com.thssh.ui.lsn09.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;

/**
 * @author zhangyugehu
 * @version V1.0
 * @date 2017/09/22
 * email: zhangyugehu@163.com
 */

public class PathHelpView extends View {

    private Paint mPaint;

    private Path mPath;

    public PathHelpView(Context context) {
        this(context, null);
    }

    public PathHelpView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PathHelpView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPath = new Path();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        pathFillType(Path.FillType.INVERSE_EVEN_ODD, canvas);
//        pathFillType(Path.FillType.EVEN_ODD, canvas);
//        pathFillType(Path.FillType.INVERSE_WINDING, canvas);
//        pathFillType(Path.FillType.WINDING, canvas);


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            pathOp(Path.Op.DIFFERENCE, canvas);
//            pathOp(Path.Op.INTERSECT, canvas);
//            pathOp(Path.Op.REVERSE_DIFFERENCE, canvas);
//            pathOp(Path.Op.UNION, canvas);
            pathOp(Path.Op.XOR, canvas);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    private void pathOp(Path.Op op, Canvas canvas) {
        Path path1 = new Path();
        Path path2 = new Path();

        path1.addCircle(150, 150, 100, Path.Direction.CW);
        path2.addCircle(200, 200, 100, Path.Direction.CW);

        path1.op(path2, op);
        canvas.drawPath(path1, mPaint);

        mPaint.setTextSize(30);
        canvas.drawText("Op." + op.name(), 50, 20, mPaint);

        mPaint.setColor(Color.DKGRAY);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(2);
        canvas.drawCircle(150, 150, 100, mPaint);
        canvas.drawCircle(200, 200, 100, mPaint);
    }

    private void pathFillType(Path.FillType type, Canvas canvas) {
        mPath.reset();
        mPaint.setTextSize(30);
        canvas.drawText("FillType." + type.name(), 100, 80, mPaint);
        mPath.offset(100, 100);
        mPath.addCircle(200, 200, 100, Path.Direction.CCW);
        mPath.addCircle(300, 300, 100, Path.Direction.CCW);
        mPath.setFillType(type);
        mPaint.setColor(Color.RED);
        canvas.drawPath(mPath, mPaint);
    }
}
